package com.ruoyi.web.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.enums.AbnormalEnum;
import com.ruoyi.framework.web.exception.TadpoleException;
import com.ruoyi.web.enums.CommonDictEnum;
import com.ruoyi.web.utils.IdWorker;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.web.mapper.UserQuestionMapper;
import com.ruoyi.web.domain.UserQuestion;
import com.ruoyi.web.service.IUserQuestionService;

/**
 * 用户提问Service业务层处理
 *
 * @author ruoyi
 * @date 2023-01-17
 */
@Service
@AllArgsConstructor
public class UserQuestionServiceImpl implements IUserQuestionService
{
    private final UserQuestionMapper userQuestionMapper;

    private final IdWorker idWorker;

    @Override
    public R<String> createQuestion(String question) {
        try {
            Long userId = SecurityUtils.getUserId();
            if (userId == null) {
                throw new TadpoleException(AbnormalEnum.MSG.OPTIONS_USER_NOT_FOUND);
            }
            userQuestionMapper.insertUserQuestion(new UserQuestion(){{
                setId(idWorker.nextId());
                setLaunchUserId(userId);
                setQuestion(question);
                setCreateTime(new Date());
                setUpdateTime(null);
                setIsAnswer(CommonDictEnum.YesOrNo.NO.getValue());
            }});
            return R.ok();
        } catch (TadpoleException exception) {
            throw exception;
        } catch (Exception e) {
            throw new TadpoleException(AbnormalEnum.MSG.DISCONNECTED);
        }
    }

    @Override
    public R<String> answerQuestion(UserQuestion question) {
        try {
            Long userId = SecurityUtils.getUserId();
            if (userId == null) {
                throw new TadpoleException(AbnormalEnum.MSG.OPTIONS_USER_NOT_FOUND);
            }
            if (question.getAnswer().isEmpty()) {
                throw new TadpoleException(AbnormalEnum.MSG.MANDATORY_PARAM_EMPTY_OR_MALFORMED.getCode(),"回复不能为空");
            }
            UserQuestion userQuestion = userQuestionMapper.selectUserQuestionById(question.getId());
            if (userQuestion == null) {
                throw new TadpoleException(AbnormalEnum.MSG.MANDATORY_PARAM_EMPTY_OR_MALFORMED.getCode(),"找不到该问题");
            }
            if(userQuestion.getIsAnswer().equals(CommonDictEnum.YesOrNo.YES.getValue())) {
                throw new TadpoleException(AbnormalEnum.MSG.MANDATORY_PARAM_EMPTY_OR_MALFORMED.getCode(),"该问题已被回复");
            }
            question.setAnswerUserId(userId);
            question.setIsAnswer(CommonDictEnum.YesOrNo.YES.getValue());
            question.setUpdateTime(new Date());
            userQuestionMapper.updateUserQuestion(question);
            return R.ok();
        } catch (TadpoleException exception) {
            throw exception;
        } catch (Exception e) {
            throw new TadpoleException(AbnormalEnum.MSG.DISCONNECTED);
        }
    }


    /**
     * 查询用户提问
     *
     * @param id 用户提问主键
     * @return 用户提问
     */
    @Override
    public UserQuestion selectUserQuestionById(Long id)
    {
        return userQuestionMapper.selectUserQuestionById(id);
    }

    /**
     * 查询用户提问列表
     *
     * @param userQuestion 用户提问
     * @return 用户提问
     */
    @Override
    public List<UserQuestion> selectUserQuestionList(UserQuestion userQuestion)
    {
        return userQuestionMapper.selectUserQuestionList(userQuestion);
    }

    /**
     * 新增用户提问
     *
     * @param userQuestion 用户提问
     * @return 结果
     */
    @Override
    public int insertUserQuestion(UserQuestion userQuestion)
    {
        userQuestion.setCreateTime(DateUtils.getNowDate());
        return userQuestionMapper.insertUserQuestion(userQuestion);
    }

    /**
     * 修改用户提问
     *
     * @param userQuestion 用户提问
     * @return 结果
     */
    @Override
    public int updateUserQuestion(UserQuestion userQuestion)
    {
        userQuestion.setUpdateTime(DateUtils.getNowDate());
        return userQuestionMapper.updateUserQuestion(userQuestion);
    }

    /**
     * 批量删除用户提问
     *
     * @param ids 需要删除的用户提问主键
     * @return 结果
     */
    @Override
    public int deleteUserQuestionByIds(Long[] ids)
    {
        return userQuestionMapper.deleteUserQuestionByIds(ids);
    }

    /**
     * 删除用户提问信息
     *
     * @param id 用户提问主键
     * @return 结果
     */
    @Override
    public int deleteUserQuestionById(Long id)
    {
        return userQuestionMapper.deleteUserQuestionById(id);
    }
}
